/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.ui.components;

import com.bff.bjj.ui.component.MusicList;
import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.lib.actions.PlaylistAddSongAction;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.DefaultListModel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.bff.javampd.MPDDatabase;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author Bill
 */
public class SongList extends MusicList implements MouseListener {

    private MPDDatabase database;
    private DefaultListModel songListModel;
    private JPopupMenu songMenu;
    private MPDPlaylist playlist;
    private Collection<MPDSong> songs;
    public static int KEY_TIMEOUT = 2000;
    public boolean keyTyping;

    public SongList() {
        songs = new ArrayList<MPDSong>();

        songListModel = new DefaultListModel();

        setDatabase(JukeboxProperties.getInstance().getDatabase());
        setPlaylist(JukeboxProperties.getInstance().getPlaylist());

        setCellRenderer(new SongListCellRenderer());
        addMouseListener(this);
    }

    public SongList(Collection<MPDSong> songs) {
        this();
        setSongs(songs);
    }

    public MPDSong getSelectedSong() {
        if (getSelectedValue() == null) {
            return null;
        }

        return (MPDSong) getSelectedValue();
    }

    public MPDDatabase getDatabase() {
        return database;
    }

    public void setDatabase(MPDDatabase database) {
        this.database = database;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            // SlimArtist artist = getSelectedArtist();
            // new SongDialog(Utils.getParentComponent(this), artist);
        } else {
            createSongPlayPopUp(e);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        createSongPlayPopUp(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        createSongPlayPopUp(e);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    private void createSongPlayPopUp(MouseEvent e) {
        if (e.isPopupTrigger()) {
            setSelectedIndex(locationToIndex(e.getPoint()));
            MPDSong song = getSelectedSong();
            songMenu = new JPopupMenu();
            songMenu.add(new JMenuItem(new PlaylistAddSongAction(getPlaylist(), song)));
            //songMenu.add(new JMenuItem(new ViewSongAction(Utils.getParentComponent(this), artist)));
            songMenu.setVisible(true);
            songMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

    public MPDPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(MPDPlaylist playlist) {
        this.playlist = playlist;
    }

    private void loadList() {
        try {
            songListModel.clear();

            for (MPDSong song : getSongs()) {
                songListModel.addElement(song);
            }

            setModel(songListModel);
            setSelectedIndex(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @return the songs
     */
    public Collection<MPDSong> getSongs() {
        return songs;
    }

    /**
     * @param songs the songs to set
     */
    public void setSongs(Collection<MPDSong> songs) {
        this.songs = songs;
        loadList();
    }
}
